package com.example.secondhandspringboot.mapper;

import com.example.secondhandspringboot.domain.entity.ChatMessages;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.secondhandspringboot.domain.vo.MessagesVO;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author 周在林
 * @since 2024-05-15
 */
public interface ChatMessagesMapper extends BaseMapper<ChatMessages> {

    /**
     * 获取最新一条消息
     * @param currentUserId
     * @param contactUserId
     * @return
     */
    ChatMessages lastNewMsg(Long currentUserId, Long contactUserId);

    int getUnReadNum(Long currentUserId, Long contactUserId);

    @Update("update chat_messages set is_read=1 where send_user=#{contactUserId} and receive_user=#{currentUserId} and is_read=0 and deleted=0")
    void updateIsRead(Long currentUserId, Long contactUserId);

    @Select("select * from chat_messages where send_user=#{currentUserId} and receive_user=#{contactUserId} or send_user=#{contactUserId} and receive_user = #{currentUserId} and deleted=0 order by create_time asc")
    List<MessagesVO> getMessages(Long currentUserId, Long contactUserId);

    @Select("select count(*) from chat_messages where receive_user = #{currentId} and is_read = 0 and deleted=0")
    int getTotalNoticeNum(Long currentId);
}
